#!/bin/bash
clear
RED="\033[31m"    # Error message
GREEN="\033[32m"  # Success message
YELLOW="\033[33m" # Warning message
BLUE="\033[36m"   # Info message
RESET='\033[0m'

if [ "$EUID" -ne 0 ]; then
  echo -e "${RED} Anda tiada kebenaran untuk menjalankan skrip ini! ${RESET}"
  exit 1
fi

apt-get -qq update
apt-get -y -qq install shadowsocks-libev
apt-get -y -qq install simple-obfs
systemctl disable shadowsocks-libev
systemctl stop shadowsocks-libev

mkdir /etc/shadowsocks-libev/clients
touch /etc/shadowsocks-libev/clients/.accounts

ipv4_addr=$(wget -qO- ipv4.icanhazip.com)
host_addr=$(cat /usr/local/.environment | grep -w 'DOMAIN' | cut -d '=' -f 2)
user_name=$(cat /usr/local/.environment | grep -w 'USERNAME' | cut -d '=' -f 2)
user_pass=$(cat /usr/local/.environment | grep -w 'PASSWORD' | cut -d '=' -f 2)

cat >/etc/shadowsocks-libev/server-default.json <<-EOF
{
  "server":"0.0.0.0",
  "server_port":6560,
  "local_port":1080,
  "password":"$user_pass",
  "method":"chacha20-ietf-poly1305",
  "mode":"tcp",
  "timeout":60,
  "fast_open":true,
}
EOF

cat >/etc/shadowsocks-libev/server-stunnel.json <<-EOF
{
  "server":"0.0.0.0",
  "server_port":6561,
  "local_port":1080,
  "password":"$user_pass",
  "method":"chacha20-ietf-poly1305",
  "mode":"tcp",
  "timeout":60,
  "fast_open":true,
}
EOF

systemctl enable shadowsocks-libev-server@server-default
systemctl start shadowsocks-libev-server@server-default
systemctl enable shadowsocks-libev-server@server-stunnel
systemctl start shadowsocks-libev-server@server-stunnel

echo
echo -e "${GREEN} Pemasangan shadowsocks-libev pakej telah selesai. ${RESET}"
echo
